<?php
/**
 * Language & LanguageException Classes
 * @filesource
 */
/**
 * Namespace
 */
namespace framework\library;
/**
 * Dependencies
 */
use framework\Front;
use framework\library\LanguageException; // At bottom of this file
/**
 * Language Class
 *
 * Provides language localization support
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses framework\Front
 */
class Language {

    private $directory;
    private $data = array();
    /**
     * Loads the base language idioms at startup, but only loads them once
     */
    public function __construct() {
        $this->registry = Front::getInstance();
        $this->config = $this->registry->get('config');
        $this->directory = $this->config->get('context_dir') . 'language/' . $this->config->get('language');
        if (!$this->registry->get('base_language_loaded')) {
            if (!$this->registry->get('text')) {
                $this->registry->set('text', array());
            }
            $this->init();
        }
    }

    /**
     * Sets the base language
     * 
     * Limits the loading of the base language to a single iteration.
     * 
     * Many controllers are instantiated during any page load. This method
     * ensures duplication does not occur in loading the base language file.
     */
    private function init() {
        $this->getLanguage('english');
        $this->registry->set('base_language_loaded', true);
    }

    /**
     * Retrieves the language idiom for a given key
     * 
     * @param type $key The name of the language idiom
     * @param array $args An array of replacement parameters for vsprintf() 
     * @return type String The localized idiom
     */
    public function getText($key, Array $args = array()) {
        $text = ($this->registry->getElement('text', $key)) ? $this->registry->getElement('text', $key) : $key;
        return (!empty($args))? vsprintf($text,$args) : $text;
    }

    /**
     * Loads a new language file
     * 
     * Searches in the context language directory first, then the system 
     * language directory, then the module specific language directory for the
     * language file to load.  If now found an error is triggered.
     * 
     * The method will search for the given language file in the follow order:
     * 1. {context_dir}/language/{lang}/{$filename}.php  <br>
     *    (ie. web/language/en/english.php)
     * 2. {SYSTEM_DIR}/language/{lang}/{$filename}.php  <br>
     *    (ie. plurious/language/en/english.php)  
     * 3. {SYSTEM_DIR}/modules/{$filename}/language/{lang}/{$filename}.php  <br>
     *    (ie. plurious/modules/googleTalk/language/en/googleTalk.php)
     * 
     * @param type $filename The filename of the file containing the localized idioms 
     */
    public function getLanguage($filename) {
        $file = $this->directory . '/' . $filename . '.php';
        if (!file_exists($file)) {
            $f1 = $file;
            $file = FRAMEWORK_DIR . 'language/' . $this->config->get('language') . '/' . $filename . '.php';
        }
        if (!file_exists($file)) {
            $f2 = $file;
            $file = SYSTEM_DIR . 'modules/' . $filename . '/language/' . $this->config->get('language') . '/' . $filename . '.php';
        }
        if (!file_exists($file)) {
            $error = 'Error: Could not load language file. Tried these file locations:<br> ';
            $error .= $file;
            $error .= '<br>';
            $error .= $f2;
            $error .= '<br>';
            $error .= $f1;
            throw new LanguageException($error);
            exit();
        }
        $_ = array();
        require($file);
        $this->registry->set('text', array_merge($this->registry->get('text'), $_));
    }

}
/**
 * LanguageException Class
 *
 * Provides exception handling support in case language files are missing
 * 
 * @author Randy Casburn <randy@plurious.com>
 * @copyright Copyright Plurious Software, all rights reserved
 * @license MIT
 * @license http://www.opensource.org/licenses/mit-license.html
 * @link http://plurious.com/framework/documentation/classes/Front.html
 * @version 2.1
 * 
 * @uses \Exception
 */

class LanguageException extends \Exception {
    public function __construct($error){
        parent::__construct($error);
        echo $this->getMessage();
//        throw new \Exception($error);;
    }
}
?>